package io;

import java.io.*;
import java.util.Scanner;

/**
 * @author haomin
 * @date 2022/06/08 15:54
 **/
public class CopyMove {
    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        System.out.println("现有电影：大鱼海棠、武侠");
        System.out.print("请输入您想拷贝的电影名：");
        String name = in.nextLine();
        System.out.print("请输入您想拷贝后的电影名：");
        String newName = in.nextLine();

        inOutStream(name,newName);

        newName = newName+"2";
        BufInOutStream1(name,newName);

    }

    private static void inOutStream(String name,String newName) throws IOException {
        long star = System.currentTimeMillis();

        String path1 = "D:\\电影\\" + name + ".mkv";
        String path2 = "D:\\电影\\" + newName + ".mkv";
        System.out.println();
        try (InputStream is = new FileInputStream(path1)){
            try (OutputStream os = new FileOutputStream(path2)){
                byte[] bur = new byte[2048];
                int n;
                while (true){
                    n = is.read(bur);
                    if(n == -1){
                        break;
                    }
                    os.write(bur,0,n);
                }
                os.flush();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("复制成功！用时：" + (end - star)/100 + "秒");
    }

    private static void BufInOutStream1(String name,String newName) throws IOException {
        long star = System.currentTimeMillis();

        String path1 = "D:\\电影\\" + name + ".mkv";
        String path2 = "D:\\电影\\" + newName + ".mkv";
        System.out.println();
        try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(path1))){
            try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(path2)) ){
                byte[] bur = new byte[1024];
                int n;
                while (true){
                    n = is.read(bur);
                    if(n == -1){
                        break;
                    }
                    os.write(bur,0,n);
                }
                os.flush();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("复制成功！用时：" + (end - star)/100 + "秒");
    }
}